<template>
  <div class="erModel">
    <div class="topBtn">
      <div style="display: flex">
        <el-button
          type="primary"
          icon="el-icon-plus"
          style="background: #2f54eb"
          @click="() => handleOperate()"
           size="small"
          >新增</el-button
        >
        <el-button
          icon="el-icon-delete"
          @click="() => handleDelete()"
          :disabled="!multipleSelection.length"
          type="danger"
           size="small"
          >删除</el-button
        >
      </div>
      <el-input
        placeholder="请输入关键字"
        v-model="queryParams.name"
        class="input-with-select"
      >
        <el-button
          slot="append"
          icon="el-icon-search"
          type="primary"
          style="background: #2f54eb"
          @click="getList"
        ></el-button>
      </el-input>
    </div>
    <div class="table">
      <el-table
        :data="tableData"
        border
        style="width: 100%"
        @selection-change="handleSelectionChange"
        size="mini"
        v-loading="loading"
      >
        <el-table-column type="selection" width="55" align="center">
        </el-table-column>
        <el-table-column label="序号" width="80" align="center">
          <template slot-scope="scope">
            {{
              (queryParams.pageNum - 1) * queryParams.pageSize +
              (scope.$index + 1)
            }}
          </template>
        </el-table-column>
        <el-table-column
          prop="name"
          label="ER模型名称"
          width="180"
          align="left"
        >
        </el-table-column>
        <el-table-column prop="mark" label="ER模型标识" align="left">
        </el-table-column>
        <el-table-column
          prop=""
          label="ER模型结构"
          align="center"
          :formatter="
            (row) =>
              row.modelStructure
                ? selectDictLabel(
                    dict.type.pt_app_er_model_structure,
                    row.modelStructure
                  )
                : '单表'
          "
        >
        </el-table-column>
        <!-- <el-table-column prop="address" label="排序" align="center" width="120">
        </el-table-column> -->
        <el-table-column prop="operate" label="操作" align="center">
          <template slot-scope="scope"
            ><div>
              <el-button type="text" @click="() => handleOperate(scope.row)"
                >编辑</el-button
              >
              <!-- <el-button type="text" @click="() => generateCode(scope.row)"
                >生成代码</el-button
              > -->
              <el-button type="text" @click="() => handleDelete(scope.row)" style="color:red"
                >删除</el-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination">
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <addOrEditModalVue
      :open="open"
      :row="row"
      :close="close"
      :appId="appId"
      v-if="open"
    />
  </div>
</template>

<script>
import {
  appErModelList,
  appErModelDelete,
} from "@/api/app/dataModel/erModel";
import addOrEditModalVue from "./addOrEditModal.vue";
export default {
  name: "erModel",
  components: {
    addOrEditModalVue,
  },
  dicts: ["pt_app_er_model_structure"],
  props: {
    appId: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      total: 0,
      queryParams: { name: undefined, pageNum: 1, pageSize: 10 },
      multipleSelection: [],
      tableData: [],
      row: {},
      open: false,
      loading: false,
    };
  },

  mounted() {
    this.getList();
  },

  methods: {
    close() {
      this.open = false;
      this.getList();
    },
    async getList() {
      this.loading = true;
      const res = await appErModelList({
        ...this.queryParams,
        appId: this.appId,
      });
      if (res.code == 200) {
        this.tableData = res.rows;
        this.total = res.total;
      }
      this.loading = false;
    },
    handleSelectionChange(arr) {
      this.multipleSelection = arr;
    },
    handleDelete(row) {
      this.$confirm(
        `此操作将永久删除${row ? "选中" : "该条"}数据, 是否继续?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(async () => {
          const ids = [];
          if (!row) {
            this.multipleSelection?.forEach((item) => ids.push(item.id));
          }
          const res = await appErModelDelete(!row ? ids.toString() : row.id);
          if (res.code == 200) {
            this.$message.success("删除成功");
            this.getList();
          }
        })
        .catch(() => {});
    },
    // 生成代码
    // generateCode(row) {},
    handleOperate(row) {
      this.row = row;
      this.open = true;
    },
  },
};
</script>

<style lang="scss" scoped>
.erModel {
  .topBtn {
    display: flex;
    justify-content: space-between;
    align-items: center;
    ::v-deep .input-with-select {
      width: 300px;
    }
    ::v-deep .el-icon-search {
      color: #fff;
    }
  }
  .table {
    margin-top: 8px;
  }
  .pagination {
    height: 50px;
  }
}
</style>
